package com.hualan.sfzy.service.course.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.entity.Course;
import com.hualan.sfzy.entity.SkilledFiled;
import com.hualan.sfzy.mapper.course.CourseMapper;
import com.hualan.sfzy.service.course.CourseService;
import com.hualan.sfzy.utils.TimeConverter;
import com.hualan.sfzy.vo.CourseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Override
    public int add(Course entity) {
        return courseMapper.insert(entity);
    }

    @Override
    public int add(CourseVo entity) {
        return courseMapper.insertVo(entity);
    }


    @Override
    public int edit(Course entity) {
        return courseMapper.update(entity);
    }

    @Override
    public int edit(CourseVo entity) {
        return courseMapper.updateVo(entity);
    }

    @Override
    public int remove(Integer id) {
        return courseMapper.delete(id);
    }

    @Override
    public int removeBatch(Integer[] ids) {
        return courseMapper.deleteBatch(ids);
    }

    @Override
    public List<Course> find() {
        return courseMapper.selectAll();
    }

    @Override
    public List<SkilledFiled> findSkilledFiled() {
        return courseMapper.selectFiled();
    }
    @Override
    public Integer[] findCounselorId(Integer courseSkillId){
        return courseMapper.selectCounselor(courseSkillId);
    }

    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, Course entity) {
        PageHelper.startPage(pageNum,pageSize);
        List<Course> courseOrders = courseMapper.select(entity);
        PageInfo<Course> pageInfo = new PageInfo<>(courseOrders);
        Map<String, Object> result = new HashMap<>();
        result.put("list", pageInfo.getList());
        result.put("total", pageInfo.getTotal());
        result.put("pageNum", pageInfo.getPageNum());
        result.put("pageSize", pageInfo.getPageSize());
        result.put("pages", pageInfo.getPages());
        return result;
    }
    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, CourseVo entity) {
        PageHelper.startPage(pageNum,pageSize);
        List<CourseVo> courseOrders = courseMapper.selectVo(entity);

        for (CourseVo course: courseOrders
             ) {if(course.getTimeStr()!=null) {
            course.setCreateTimeStr(TimeConverter.toMinSecStr(course.getTimeStr()));
        }else {
                 course.setTimeStr(0);
            course.setCreateTimeStr(TimeConverter.toMinSecStr(0));
        }
        }
        PageInfo<CourseVo> pageInfo = new PageInfo<>(courseOrders);
        Map<String, Object> result = new HashMap<>();
        result.put("list", pageInfo.getList());
        result.put("total", pageInfo.getTotal());
        result.put("pageNum", pageInfo.getPageNum());
        result.put("pageSize", pageInfo.getPageSize());
        result.put("pages", pageInfo.getPages());
        return result;
    }

    @Override
    public int editStudyNum(Integer courseId, Integer num) {
        return courseMapper.updateStudyNum(courseId,num);
    }


    @Override
    public Course findById(Integer id) {
        return courseMapper.selectById(id);
    }
}
